<?php
    
    require_once("Common/InputValidationHandler.php");
	require_once("Common/SaltHandler.php");
    
    class SignUpView{
    		
		const USERNAME = "UserName";
		const PASSWORD = "PassWord";
		const PASSWORD_CONF = "PassConf";
		const EMAIL = "Email";
		const ACCEPT = "Accept";
		const SUBMIT = "Submit";
		const ADMIN = "Admin";
		
		private $message;
			
    	public function __construct(){
    	}
		
		/**
		 * public function to generate the signup form
		 * @param Bool if admin
		 * @return HTML
		 */
		public function generateSignUpBox($isAdmin = false){
			if($isAdmin){
				$returnBox = "
					<div class ='signupDiv'>
						<form id='form' action='' method='post'>
							<fieldset>			
							    <legend><strong>Create a new user</strong></legend>
							    $this->message
							    </br>
							    <label for='UserName'>Enter Username:</label>
							    <input class='user' type='text' size='8' id='UserName' name='".self::USERNAME."' /><br />
							    
							    <label for='PassWord'>Enter Password:</label>
							    <input class='pass' type='password' size='8' id='PassWord' name='".self::PASSWORD."' /><br />
							    
							    <label for='PassWord'>Confirm Password:</label>
							    <input class='pass' type='password' size='8' id='PassWord' name='".self::PASSWORD_CONF."' /><br />
							    
							    <label for='Email'>Enter E-Mail:</label>
							    <input class='email' type='text' size='30' id='Email' name='".self::EMAIL."'/><br />
							    
							    <input type='checkbox' name='".self::ADMIN."' class='textbox' text='true' >New Admin?</input><br />
							    
							    <input type='Submit' name='".self::SUBMIT."' value='Create!'/>
						  </fieldset>
						</form>
					</div>";
			}	
			else{
				$returnBox = "
					<div class ='signupDiv'>
						<form id='form' action='' method='post'>
							<fieldset>			
							    <legend><strong>Sign up for a Yaic account now!</strong></legend>
							    $this->message
							    </br>
							    <label for='UserName'>Enter Username:</label>
							    <input class='user' type='text' size='8' id='UserName' name='".self::USERNAME."' /><br />
							    
							    <label for='PassWord'>Enter Password:</label>
							    <input class='pass' type='password' size='8' id='PassWord' name='".self::PASSWORD."' /><br />
							    
							    <label for='PassWord'>Confirm Password:</label>
							    <input class='pass' type='password' size='8' id='PassWord' name='".self::PASSWORD_CONF."' /><br />
							    
							    <label for='Email'>Enter E-Mail:</label>
							    <input class='email' type='text' size='30' id='Email' name='".self::EMAIL."'/><br />
							    <a href='#' alt='Terms' class='terms'>Terms</a>
							    <input type='checkbox' name='".self::ACCEPT."' class='textbox' text='Accept terms'>Accept terms!</input><br />
							    
							    <input type='Submit' name='".self::SUBMIT."' value='Sign up!'/>
						  </fieldset>
						</form>
					</div>";
			}
			
			return $returnBox;
		}
		
		/**
		 * public function lets us know if user clicket submit
		 * @param -
		 * @return Bool
		 */
		public function didUserClickSubmit(){
			if(isset($_POST[self::SUBMIT])){
				return true;
			}
			return false;
		}
		
		/**
		 * private function to make sure user filled all fields
		 * @param -
		 * @return Bool
		 */
		private function areAllFieldsFilled(){
			if(empty($_POST[self::USERNAME]) || empty($_POST[self::PASSWORD]) || empty($_POST[self::PASSWORD_CONF]) || empty($_POST[self::EMAIL])){
				return false;
			}
			return true;
		}
		
		/**
		 * private function to make sure user entered the same password twice
		 * @param -
		 * @return Bool
		 */
		private function isPasswordConfirmed(){
			if(isset($_POST[self::PASSWORD])){
				if($_POST[self::PASSWORD] === $_POST[self::PASSWORD_CONF]){
					if(strlen($_POST[self::PASSWORD]) >1 ){
						return true;
					}
				}
			}
			
			return false;
		}
		
		/**
		 * private function to make sure user accepted our terms
		 * If Admin, terms are allways accepted
		 * @param Bool if admin registered the new user
		 * @return Bool
		 */
		private function didUserAcceptTerms($isAdmin){
			if($isAdmin){
				return true;
			}
			if(isset($_POST[self::ACCEPT])){
				return true;
			}
			return false;
		}
		
		/**
		 * Public function that makes sure the form is correct
		 * @param Bool if admin registered the new user
		 * @return bool
		 */
		public function isFormValid($isAdmin){
			$returnBool = true;
			
			//Values in all fields?
			if($this->areAllFieldsFilled() === false){
				$this->message .= "<p class='errorP'>Ooops... you missed a field!</p>";
				$returnBool = false;
			}
			
			//Password confirmed?
			if($this->isPasswordConfirmed() === false){
				$this->message .= "<p class='errorP'>Ooops... your passwords does not match!</p>";
				$returnBool = false;
			}
			
			//Password validation check
			elseif(InputValidationHandler::isValidPass($_POST[self::PASSWORD]) === false){
				$this->message .= "<p class='errorP'>Ooops... your passwords is not valid!</p>";
				$returnBool = false;
			}
			
			//Username validation check
			If(InputValidationHandler::isValidUser($_POST[self::USERNAME]) === false){
				$this->message .= "<p class='errorP'>Ooops... your Username is not valid!</p>";
				$returnBool = false;
			}
			
			//Email validation check
			if(InputValidationHandler::isValidEmail($_POST[self::EMAIL]) === false){
				$this->message .= "<p class='errorP'>Ooops... your Email is not valid!</p>";
				$returnBool = false;
			}
			
			//Did user accept our terms?
			if($this->didUserAcceptTerms($isAdmin) === false){
				$this->message .= "<p class='errorP'>Ooops... you did not accept our terms!</p>";	
				$returnBool = false;
			}
			
			return $returnBool;
		}
		
		/**
		 * Public function to set error message if username allready exists
		 * @param -
		 * @return -
		 */
		public function SetErrorUserNameExisted(){
			$this->message .= "<p class='errorP'>Ooops... The Username allready exists!</p>";
		}
		
		/**
		 * Public function to get userInfo when user signsup
		 * @Param -
		 * @return User-object
		 */
		public function getUserCredentials(){
			$salt = SaltHandler::getRandomSalt();
			$admin;
			
			if(isset($_POST[self::ADMIN])){
				$admin = true;
			} 
			else{
				$admin = false;
			}
			
			$user = User::create(0, $_POST[self::USERNAME], crypt($_POST[self::PASSWORD], $salt), $_POST[self::EMAIL], $salt, $admin);	
	      	return $user;
		}
    }
    
    
    